package com.hzqy.web.ac.epg;
import com.hzqy.commons.utils.ExcelUtil;
import com.hzqy.commons.utils.ExcelUtil.MyEntry;
import com.hzqy.service.epg.AcStbUserVoteNumService;
import com.hzqy.service.epg.AcSvcVoteToTalService;
import com.hzqy.web.vo.AcVoteUserLikeVo;
import com.hzqy.web.vo.CmVoteTotalVo;
import com.hzqy.web.vo.epg.AcStbUserVoteNumVo;
import com.hzqy.web.vo.epg.AcTicketNumVo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/acSvcVoteTotal")
public class AcSvcVoteTotalAction {
    @Resource
	private AcSvcVoteToTalService serviceAction;
    @Resource
	private AcStbUserVoteNumService numService;
    
	@RequestMapping("/updateAc")
	@ResponseBody
	public Object updateAc(CmVoteTotalVo aaVo){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AcStbUserVoteNumVo numvo=new AcStbUserVoteNumVo();
		numvo.setF_asuv_activityid(String.valueOf(aaVo.getF_csvt_activityid()));
		numvo.setF_asuv_serverid(aaVo.getF_csvt_obligate1());
		numvo.setF_asuv_votenum(String.valueOf(aaVo.getF_csvt_votecount()));
		numvo.setF_asuv_source("998");
		numvo.setF_asuv_memo("后台操作输入");
		numvo.setF_asuv_obligate1(0);
		numService.insetAcVoteNum(numvo);
		int i=serviceAction.updateAc(aaVo);
		if(i > 0){
			resultMap.put("success", true);
			resultMap.put("result", "修改成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result", "修改失败!flag=" + i);
		}
		return resultMap;
	}
	@RequestMapping("/insertAc")
	@ResponseBody
	public Object insertAc(CmVoteTotalVo aaVo){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int serverids=serviceAction.acSvcVoteTotalCount(aaVo);
		if(serverids<=0){
			AcStbUserVoteNumVo numvo=new AcStbUserVoteNumVo();
			numvo.setF_asuv_activityid(String.valueOf(aaVo.getF_csvt_activityid()));
			numvo.setF_asuv_serverid(aaVo.getF_csvt_obligate1());
			numvo.setF_asuv_votenum(String.valueOf(aaVo.getF_csvt_votecount()));
			numvo.setF_asuv_source("998");
			numvo.setF_asuv_memo("后台操作输入");
			numvo.setF_asuv_obligate1(0);
			numService.insetAcVoteNum(numvo);
			int i=serviceAction.insertAc(aaVo);
			if(i > 0){
				resultMap.put("success", true);
				resultMap.put("result", "新增成功!");
			} else {
				resultMap.put("success", false);
				resultMap.put("result", "新增失败!flag=" + i);
			}
		}else{//该用户已参加该活动
			resultMap.put("success", false);
			resultMap.put("result", "新增失败!该用户已参与该活动!");
		}
		return resultMap;
	}
	
	@RequestMapping("/acSvcVoteTotalList")
	@ResponseBody
	public Object acSvcVoteTotalList(CmVoteTotalVo aaVo,int page, int rows ){
		return serviceAction.acSvcVoteTotalList(aaVo,page,rows);
	}
	@RequestMapping("/selectAcInfo")
	@ResponseBody
	public Object selectAcInfo(CmVoteTotalVo aaVo){
		return serviceAction.selectAcInfo(aaVo);
	}
	@RequestMapping("/acSvcVoteTotalExcel")
	@ResponseBody
	public void acTicketExcel(CmVoteTotalVo aaVo, HttpSession session, int page, int rows, HttpServletResponse response) throws IOException {
		Map<String,Object> ListMap = new HashMap<String, Object>();
    	ListMap = serviceAction.acSvcVoteTotalList(aaVo,page, rows );
		List<CmVoteTotalVo> data=(List<CmVoteTotalVo>)ListMap.get("rows");
		List<MyEntry<String, String>> excelHeader=new ArrayList<MyEntry<String, String>>();
		excelHeader.add(new MyEntry<String,String>("f_csvt_obligate1","业务帐号"));
		excelHeader.add(new MyEntry<String,String>("f_csvt_votecount","用户票数"));
		excelHeader.add(new MyEntry<String,String>("f_csvt_activityid","活动编号"));
		excelHeader.add(new MyEntry<String,String>("f_csvt_cdate","创建时间"));
		excelHeader.add(new MyEntry<String,String>("f_csvt_udate","更新时间"));
		boolean flag= ExcelUtil.generateExcel(data, excelHeader, response);
	}

}
